import os
import time

# 只压缩文件夹，却没有压缩文件.
# 需要备份起始文件夹
source_dir = 'C:\\Users\\GaoMing\\Desktop\\0000\\'


# 备份保存到
save_dir = 'E:\\Ziper\\'

# 排除的指定文件夹
ignore_folder = ['.vscode', 'node_modules', '.git', '.svn']

# 排除的任意指定文件
ignore_files = ['*.log', '*.md', '*.tmp' ,'*.html']

# 排除指定路径下的文件(会保留相应路径下的文件夹)
ignore_path_files = ['app/*', 'log/*', 'runtime/session/*',
                     'runtime/config/*', 'development.js', "Log"]

ignore_str = ''
for i in ignore_files:
    ignore_str = ignore_str+'-x!'+i+' '

for i in ignore_folder:
    ignore_str = ignore_str+'-xr!*'+i+' '

for i in ignore_path_files:
    ignore_str = ignore_str+'-x!'+i+' '

#  ==================== 将文件压缩 ===============
# 获取需要压缩的项目文件夹
dirs = os.listdir(source_dir)
# dirs=["demo1","demo1"] # 或者自定义项目名

print("dirs:{}".format(dirs))
print('*'*50)

# 指定不压缩的文件夹
skip_dirs = ['更新说明', '历史备份', '杂项']

print('--------dirs-------:',dirs)

for d in dirs:
    if d in skip_dirs:
        continue
    # 构建文件夹如今，os.sep 获取的是 \\
    d_path = source_dir+d+os.sep

    print('d_path:',d_path)
    if os.path.isdir(d_path):
        # 拼接文件名
        fname = d+"@" + time.strftime('%Y-%m-%d-%H-%M')+'.7z'
        # 拼接保存文件路径
        target = save_dir + os.sep + fname
        # 拼接命令（按需要自行修改）
        # print('996:',os.getcwd())
        cmd = "7z.exe a -t7z %s %s%s* -r %s -mx=5 -m0=LZMA2 -ms=10m -mf=on -mhc=on -mmt=on" % (target, d_path, os.sep, ignore_str)
        # print(cmd)
        # 执行压缩
        os.system(cmd)
        print(source_dir, '=====>', target)
    else:
        print('')
